package icu.javayanglei.entity;

import icu.javayanglei.enumeration.ResultCode;
import lombok.Data;


import java.io.Serializable;

/**
 * 返回状态码
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-11-21 23:57
 */

@Data
public class Result implements Serializable {
    private Integer code;
    private String message;
    private Object data;

    public Result(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result() {
    }

    public Result(ResultCode resultCode, Object data) {
        this.code = resultCode.code();
        this.message = resultCode.message();
        this.data = data;
    }

    public Result(ResultCode resultCode) {
        this.code = resultCode.code();
        this.message = resultCode.message();
    }

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 返回成功！
     *
     * @return 成功
     */
    public static Result success() {
        final Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 返回成功！
     *
     * @param data 数据
     * @return Result，成功！
     */
    public static Result success(Object data) {
        final Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 返回失败！
     *
     * @param resultCode 失败枚举
     * @return 失败！
     */
    public static Result failure(ResultCode resultCode) {
        final Result result = new Result();
        result.setResultCode(resultCode);
        return result;
    }

    public static Result failure(Integer code, String msg, Object data) {
        return new Result(code, msg, data);
    }

    public static Result failure(Integer code, String msg) {
        return new Result(code, msg);
    }

    /**
     * 返回失败！
     *
     * @param resultCode 失败枚举
     * @param data       失败数据
     * @return 失败！
     */
    public static Result failure(ResultCode resultCode, Object data) {
        final Result result = new Result();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }


    public void setResultCode(ResultCode resultCode) {
        this.code = resultCode.code();
        this.message = resultCode.message();
    }


}
